import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-10-02
 * Time: 20:39
 */
public class MyStack {

    //stack 底层是数组 -- 先进栈得后出 后进栈得先出
    public int [] elme;

    public int usedsize;

    public static final int DEFAULT_SIZE = 5;

    public MyStack() {
        this.elme = new int [DEFAULT_SIZE];
    }

    //压栈
    public int push(int val){
        //1.栈满 越界

        if(isfull()){
            this.elme = Arrays.copyOf(this.elme,2*this.elme.length);
        }
        this.elme[usedsize++] = val;

        return val;

    }

    private boolean isfull(){
        return usedsize>=this.elme.length;
    }

    //出栈
    public int pop(){
        if(empty()){
            throw new EmptyException("栈为空，无法出栈！");
        }
        return this.elme[--usedsize];
    }

    public boolean empty(){
        return usedsize == 0;
    }

    //查看栈顶数据
    public int peek(){
        if(empty()){
            throw new EmptyException("栈为空！");
        }
        return this.elme[usedsize-1];
    }

}
